from django.shortcuts import render_to_response, Http404
from django.template import RequestContext
from django.http import HttpResponseRedirect

from apps.course.functions import course_reverse
from apps.participants.models import CourseParticipant
from apps.management import Management
from models import *

def manage(request, TheCourseParticipant, TheCourse):
    permissions = {}
    permissions['add_announcement'] = request.user.has_perm('news.add_announcement')
    permissions['change_announcement'] = request.user.has_perm('news.change_announcement')
    permissions['add_event'] = request.user.has_perm('news.add_event')
    permissions['change_event'] = request.user.has_perm('news.change_event')
    if TheCourseParticipant:
        permissions['add_announcement'] = permissions['add_announcement'] or TheCourseParticipant.status == "INSTRUCTOR" or TheCourseParticipant.status == "ASSISTANT"
        permissions['change_announcement'] = permissions['change_announcement'] or TheCourseParticipant.status == "INSTRUCTOR" or TheCourseParticipant.status == "ASSISTANT"
        permissions['add_event'] = permissions['add_event'] or TheCourseParticipant.status == "INSTRUCTOR" or TheCourseParticipant.status == "ASSISTANT"
        permissions['change_event'] = permissions['change_event'] or TheCourseParticipant.status == "INSTRUCTOR" or TheCourseParticipant.status == "ASSISTANT"

    return render_to_response('news/manage.html', {'announcements':  GetAnnouncements(TheCourse.course_id),
                'events':  GetEvents(TheCourse.course_id), 'pastevents':GetPastEvents(TheCourse.course_id), 'permissions':permissions}, context_instance=RequestContext(request))
Management.register_interface('News','ASSISTANT;INSTRUCTOR','manage',manage)

def FindAnnouncement(TheCourse, type, id=None):
    if id:
        if type == 'announcement': Object = Announcement.objects.filter(pk=id)
        elif type == 'event': Object = Event.objects.filter(pk=id)
        if Object: return Object[0]
    return None

def edit(request, TheCourseParticipant, TheCourse, type, id=None):
    if type == 'announcement': template = 'news/edit_announcement.html'
    elif type == 'event': template = 'news/edit_event.html'
    else:
        logger.info("type %s not found",type)
        raise Http404
    
    Object = None
    if id:
        if type == 'announcement': Object = Announcement.objects.filter(pk=id)
        elif type == 'event': Object = Event.objects.filter(pk=id)
        if Object: Object = Object[0]
        else:
            logger.info("%s with id %d not found",type, id)
            raise Http404
        
    if request.method == 'POST':
        if type == 'announcement': form = AnnouncementForm(request.POST,instance=Object,course=TheCourse.course_id)
        else: form = EventForm(request.POST,instance=Object,course=TheCourse.course_id)
        
        if (form.is_valid()):
            form.save()
            return HttpResponseRedirect(course_reverse('management',TheCourse.course_id,app='news'))
    else:
        if type == 'announcement': form = AnnouncementForm(instance=Object,course=TheCourse.course_id)
        else: form = EventForm(instance=Object,course=TheCourse.course_id)
    
    return render_to_response(template, {'form':form}, context_instance=RequestContext(request)) 
Management.register_interface('News','ASSISTANT;INSTRUCTOR','edit',edit)